package cn.ac.iie.di.ban.jr.swb.rest.mq.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Black-Laptop on 8/18/2017.
 */
public class utils {

    public static Logger LOG = Logger.getLogger(utils.class);
    public static String datatype;

    public static List<Object[]> getData(String datatype, JSONObject jsonObject, List<String> colNameList) {
        LOG.info("getData");
        utils.datatype = datatype;
        List<Object[]> objectsList = new ArrayList<>();

        JSONObject sourceJson = JSONObject.parseObject(String.valueOf(jsonObject.getJSONObject("source")));
        if (sourceJson == null) {
            LOG.error(datatype + " no column: sourceJson");
        }
        //1.热门微博的data为数组
        JSONObject dataJson = JSONObject.parseObject(String.valueOf(sourceJson.getJSONObject("data")));
        if (dataJson == null) {
            LOG.error(datatype + " no column: dataJson");
        }
        Object createTime = dataJson.getString("createtime");
        if (createTime == null) {
            LOG.error(datatype + " no column: createTime");
        }
        for (Object subJson : dataJson.getJSONArray("list")) {
            Object[] objects = getSingleDataDayWeekArry(JSONObject.parseObject(String.valueOf(subJson)), colNameList, createTime);
            objectsList.add(objects);
        }

        return objectsList;
    }

    /**
     *
     * @param datatype 新浪发送来的数据类型，以1.热门微博为例
     * @param jsonObject 新浪发送来的json请求
     * @param colNameList 消息队列的列名集合
     * @param gatherTime 数据采集时间，我填
     * @param insertTime 数据入库时间，我不填
     * @return
     */
    public static List<Object[]> getTHotData(String datatype, JSONObject jsonObject, List<String> colNameList, long gatherTime, long insertTime) {
        LOG.info("getTHotData :" + datatype);
        List<Object[]> objectsList = new ArrayList<>();
        try {

            utils.datatype = datatype;
            //1.热门微博的data为数组1.热门微博(t_hot_microblog)接口2.大V热门微博(t_vhot_microblog)接口
            if (datatype.equals("hot_microblog") || datatype.equals("vhot_microblog")) {
                JSONArray dataJson = jsonObject.getJSONArray("data");
                objectsList = getDataArray(dataJson, colNameList, gatherTime, insertTime);
            } else if (datatype.equals("multiple_topic") || datatype.equals("class_topic")) {
                //3.热门话题（综合t_ multiple_topic）4.热门话题（分类t_class_topic）
                Object[] obj = getHotHuati(jsonObject, colNameList, gatherTime, insertTime);
                objectsList.add(obj);
            } else if (datatype.equals("orig_microblog") || datatype.equals("vorig_microblog")) {
                //6.日原创微博量(t_orig_microblog)7.大V日原创微博量(t_vorig_microblog)
                Object[] obj = getOrign(jsonObject, colNameList, gatherTime, insertTime);
                objectsList.add(obj);
            } else if (datatype.equals("online_user")) {
                //8.在线用户数(t_online_user)
                Object[] obj = getOnlineUser(jsonObject, colNameList, gatherTime, insertTime);
                objectsList.add(obj);
            } else {
                //5热搜词(t_hot_search)
                Object[] obj = getHotSearch(jsonObject, colNameList, gatherTime, insertTime);
                objectsList.add(obj);
            }

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return objectsList;
    }

    public static List<Object[]> getDataArray(JSONArray dataJson, List<String> colNameList, long gatherTime, long insertTime) {
        List<Object[]> objectsList = new ArrayList<>();

        try {
            for (int i = 0; i < dataJson.size(); i++) {
                JSONObject jsonObject = dataJson.getJSONObject(i);
                List<Object> objectList = new ArrayList<>();
                for (String column : colNameList) {
                    LOG.debug("get " + column + " data");
                    Object object = null;
                    switch (column) {
                        case "CONTENT":
                            object = jsonObject.getString("content");
                            break;
                        case "UID":
                            object = jsonObject.getString("uid");
                            break;
                        case "TIME":
                            //按理应该为时间戳 long类型
                            String datetime = jsonObject.getString("time");
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Date date = simpleDateFormat.parse(datetime);
                            long ts = date.getTime();
                            object = ts / 1000;
                            break;
                        case "MID":
                            object = jsonObject.getString("mid");
                            break;
                        case "URL":
                            object = jsonObject.getString("url");
                            break;
                        case "SOURCE":
                            object = jsonObject.getString("source");
                            break;
                        case "GATHERTIME":
                            //将两个时间添加
                            object = gatherTime;
                            break;
                        case "INSERTTIME":
                            //将两个时间添加
                            object = insertTime;
                            break;
                        default:
                            LOG.warn("not such column ");
                            break;
                    }
                    objectList.add(object);
                }//一个数据
                objectsList.add(objectList.toArray());
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return objectsList;
    }

    public static Object[] getHotHuati(JSONObject jsonObject, List<String> colNameList, long gatherTime, long insertTime) {
        List<Object> objectList = new ArrayList<>();
        String code = jsonObject.getString("code");
        if (code.equals("200")) {
            JSONObject data = jsonObject.getJSONObject("data");
            try {

                for (String column : colNameList) {
                    LOG.debug("get " + column + " data");
                    Object object = null;
                    switch (column) {
                        case "GATHERTIME":
                            //将两个时间添加
                            object = gatherTime;
                            break;
                        case "INSERTTIME":
                            //将两个时间添加
                            object = insertTime;
                            break;
                        //热门话题的字段
                        case "DATE":
                            if (utils.datatype.equals("class_topic")) {
                                //分类话题按照string存
                                object = data.getString("date");
                            } else {
                                String huatidatetime = data.getString("date");
                                SimpleDateFormat simpleDateFormat_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                Date date_1 = simpleDateFormat_1.parse(huatidatetime);
                                object = date_1.getTime() / 1000;
                            }
                            break;
                        case "HOT_HUATI":
                            object = data.getString("hot_huati");
                            break;
                        case "CTG_TOPIC":
                            object = data.getString("ctg_topic");
                            break;
                        case "HOT_SEARCH":
                            object = data.getString("hot_search");
                            break;
                        case "M_TYPE"://新加话题分类12-01
                            object = jsonObject.getString("m_type");

                    }
                    objectList.add(object);
                }

            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
        } else {
            LOG.warn("code id not 200");
        }
        return objectList.toArray();
    }

    public static Object[] getOrign(JSONObject jsonObject, List<String> colNameList, long gatherTime, long insertTime) {
        List<Object> objectList = new ArrayList<>();
        try {
            for (String column : colNameList) {
                LOG.debug("get " + column + " data");
                Object object = null;
                switch (column) {
                    case "GATHERTIME":
                        //将两个时间添加
                        object = gatherTime;
                        break;
                    case "INSERTTIME":
                        //将两个时间添加
                        object = insertTime;
                        break;
                    //热门话题的字段
                    case "DATA":
                        object = Integer.parseInt(jsonObject.getString("data"));
                        break;
                }
                objectList.add(object);
            }

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        return objectList.toArray();
    }

    public static Object[] getOnlineUser(JSONObject jsonObject, List<String> colNameList, long gatherTime, long insertTime) {
        List<Object> objectList = new ArrayList<>();
        try {
            for (String column : colNameList) {
                LOG.debug("get " + column + " data");
                Object object = null;
                switch (column) {
                    case "GATHERTIME":
                        //将两个时间添加
                        object = gatherTime;
                        break;
                    case "INSERTTIME":
                        //将两个时间添加
                        object = insertTime;
                        break;
                    case "TOTAL":
                        JSONObject total = jsonObject.getJSONObject("total");
                        object = total.getIntValue("num");
                        break;
                    case "DATE_TIME":
                        String searchdatetime = jsonObject.getString("datetime");
                        SimpleDateFormat simpleDateFormat_2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date_2 = simpleDateFormat_2.parse(searchdatetime);
                        object = date_2.getTime() / 1000;
                        break;
                    case "PC_NUM":
                        JSONObject pc = jsonObject.getJSONObject("pc");
                        object = pc.getIntValue("num");
                        break;
                    case "PC_TIME":
                        JSONObject pc_1 = jsonObject.getJSONObject("pc");
                        SimpleDateFormat simpleDateFormat_3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date_3 = simpleDateFormat_3.parse(pc_1.getString("time"));
                        object = date_3.getTime() / 1000;
                        break;
                    case "MOBILE_NUM":
                        JSONObject mobile = jsonObject.getJSONObject("mobile");
                        object = mobile.getIntValue("num");
                        break;
                    case "MOBILE_TIME":
                        JSONObject mobile_1 = jsonObject.getJSONObject("mobile");
                        SimpleDateFormat simpleDateFormat_5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date_5 = simpleDateFormat_5.parse(mobile_1.getString("time"));
                        object = date_5.getTime() / 1000;
                        break;
                }
                objectList.add(object);
            }

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        return objectList.toArray();
    }

    public static Object[] getHotSearch(JSONObject jsonObject, List<String> colNameList, long gatherTime, long insertTime) {
        List<Object> objectList = new ArrayList<>();
        try {
            for (String column : colNameList) {
                LOG.debug("get " + column + " data");
                Object object = null;
                switch (column) {
                    case "GATHERTIME":
                        //将两个时间添加
                        object = gatherTime;
                        break;
                    case "INSERTTIME":
                        //将两个时间添加
                        object = insertTime;
                        break;
                    //热门话题的字段
                    case "DATA":
                        object = Integer.parseInt(jsonObject.getString("data"));
                        break;
                    case "HOT_SEARCH":
                        object = jsonObject.getString("hot_search");
                        break;
                    case "DATETIME":

                        String searchdatetime = jsonObject.getString("datetime");
                        SimpleDateFormat simpleDateFormat_2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date_2 = simpleDateFormat_2.parse(searchdatetime);
                        object = date_2.getTime() / 1000;

                        break;
                }
                objectList.add(object);
            }

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        return objectList.toArray();
    }

    /**
     * wb数据对应统一接口
     *
     * @param jsonObject
     * @param colNameList
     * @param createTime
     * @return
     */
    public static Object[] getWBArray(JSONObject jsonObject, List<String> colNameList, long gatherTime, long insertTime, Object sendtime) {
        List<Object> objectList = new ArrayList<>();
        try {

            for (String column : colNameList) {
                LOG.debug("get " + column + " data");
                Object object = null;
                switch (column) {
                    case "CONTENT":
                        object = jsonObject.getString("content");
                        break;
                    case "UID":
                        object = jsonObject.getString("uid");
                        break;
                    case "TIME":
                        //按理应该为时间戳 long类型
                        String datetime = jsonObject.getString("time");
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = simpleDateFormat.parse(datetime);
                        long ts = date.getTime();
                        object = ts / 1000;
                        break;
                    case "MID":
                        object = jsonObject.getString("mid");
                        break;
                    case "URL":
                        object = jsonObject.getString("url");
                        break;
                    case "SOURCE":
                        object = jsonObject.getString("source");
                        break;
                    case "GATHERTIME":
                        //将两个时间添加
                        object = gatherTime;
                        break;
                    case "INSERTTIME":
                        //将两个时间添加
                        object = insertTime;
                        break;
                    //热门话题的字段
                    case "DATE":
                        String huatidatetime = jsonObject.getString("date");
                        SimpleDateFormat simpleDateFormat_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date_1 = simpleDateFormat_1.parse(huatidatetime);
                        object = date_1.getTime() / 1000;
                        break;
                    case "HOT_HUATI":
                        object = jsonObject.getString("hot_huati");
                        break;
                    case "CTG_TOPIC":
                        object = jsonObject.getString("ctg_topic");
                        break;
                    case "HOT_SEARCH":
                        object = jsonObject.getString("hot_search");
                        break;
                    case "DATETIME":
                        if (datatype.equals("wb_num") || datatype.equals("vwb_num")) {
                            object = Long.parseLong((String) sendtime);
                        } else {
                            String searchdatetime = jsonObject.getString("datetime");
                            SimpleDateFormat simpleDateFormat_2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Date date_2 = simpleDateFormat_2.parse(searchdatetime);
                            object = date_2.getTime() / 1000;
                        }
                        break;
                    case "DATA":
                        object = Integer.parseInt(jsonObject.getString("data"));
                        break;

                }
                objectList.add(object);
            }

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return objectList.toArray();
    }

    public static Object[] getWBOLUSArray(JSONObject sourceJsonObject, List<String> colNameList, long gatherTime, long insertTime) {
        List<Object> objectList = new ArrayList<>();
        try {
            String mobile = sourceJsonObject.getString("mobile");
            JSONObject mobileJSONObject = JSONObject.parseObject(mobile);
            String pc = sourceJsonObject.getString("pc");
            JSONObject pcJSONObject = JSONObject.parseObject(pc);
            String total = sourceJsonObject.getString("total");
            JSONObject totalJSONObject = JSONObject.parseObject(total);
            for (String colName : colNameList) {
                Object object = null;
                switch (colName) {
                    case "PC_NUM":
                        object = pcJSONObject.getString("num");
                        break;
                    case "TIME":
                        String searchdatetime = pcJSONObject.getString("time");
                        SimpleDateFormat simpleDateFormat_2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date_2 = simpleDateFormat_2.parse(searchdatetime);
                        object = date_2.getTime() / 1000;
                        break;
                    case "TOTAL":
                        object = totalJSONObject.getString("num");
                        break;
                    case "MOBILE_NUM":
                        object = mobileJSONObject.getString("num");
                        break;
                    case "C_TIME":
                        String searchdatetime_1 = pcJSONObject.getString("time");
                        SimpleDateFormat simpleDateFormat_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date_1 = simpleDateFormat_1.parse(searchdatetime_1);
                        object = date_1.getTime() / 1000;
                        break;
                    case "GATHERTIME":
                        //将两个时间添加
                        object = gatherTime;
                        break;
                    case "INSERTTIME":
                        //将两个时间添加
                        object = insertTime;
                        break;
                }
                objectList.add(object);
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return objectList.toArray();
    }

    public static Object[] getSingleDataDayWeekArry(JSONObject jsonObject, List<String> colNameList, Object createTime) {
        List<Object> objectList = new ArrayList<>();
        for (String column : colNameList) {
            LOG.debug("get " + column + " data");
            Object object = null;
            switch (column) {
                case "CREATETIME": {
                    object = createTime;
                    break;
                }
                case "SORTID": {
                    object = jsonObject.getInteger("sortId");
                    if (object == null) {
                        LOG.error(datatype + " no column: sortId");
                    }
                    break;
                }
                case "TITLE": {
//                    object = jsonObject.getString("Title");
//                    if (object == null) {
//                        LOG.error(datatype + " no column: Title");
//                    }
                    String str = jsonObject.getString("Title");
                    if (str == null) {
                        LOG.error("no column: Title");
                    }
                    str = str.replace("\\\\r\\\\n", "");
                    str = str.replace(" ", "+");
                    byte[] ansBytes = Base64.decodeBase64(str);
                    try {
                        object = new String(ansBytes, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        LOG.error(ExceptionUtils.getFullStackTrace(e));
                    }
                    break;
                }
                case "CONTENTS": {
                    String str = jsonObject.getString("content");
                    if (str == null) {
                        LOG.error(datatype + " no column: content");
                    }
                    str = str.replace("\\\\r\\\\n", "");
                    str = str.replace(" ", "+");
                    byte[] ansBytes = Base64.decodeBase64(str);
                    try {
                        object = new String(ansBytes, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        LOG.error(ExceptionUtils.getFullStackTrace(e));
                    }
                    break;
                }
                case "URL": {
//                    object = jsonObject.getString("url");
//                    if (object == null) {
//                        LOG.error(datatype + " no column: url");
//                    }
                    String str = jsonObject.getString("url");
                    if (str == null) {
                        LOG.error("no column: url");
                    }
                    str = str.replace("\\\\r\\\\n", "");
                    str = str.replace(" ", "+");
                    byte[] ansBytes = Base64.decodeBase64(str);
                    try {
                        object = new String(ansBytes, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        LOG.error(ExceptionUtils.getFullStackTrace(e));
                    }
                    break;
                }
                case "PVIEWS": {
                    object = -1;
                    break;
                }
                case "LIKENUM": {
                    object = -1;
                    break;
                }
                case "HOTVALUE": {
                    object = jsonObject.getString("hotValue");
                    if (object == null) {
                        LOG.error(datatype + " no column: hotValue");
                    }
                    break;
                }
                case "HOT_SORT": {
                    String str = jsonObject.getString("type");
                    if (str == null) {
                        LOG.error(datatype + " no column: type");
                    }
                    str = str.replace("\n", "");
                    object = str;
                    break;
                }
                case "ENTERINGTIME": {
                    object = "";
                    break;
                }
            }
            objectList.add(object);
        }
        return objectList.toArray();
    }
}
